import { Module } from '@nestjs/common';
import { SequelizeModule } from '@nestjs/sequelize';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
import { AppController } from './app.controller';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Dialect } from 'sequelize/types';
import { AuditModule } from './audit/audit.module';
import { ProjectModule } from './project/project.module';
import { VideoModule } from './video/video.module';
import { ProjectVideoModule } from './project-video/project-video.module';
import { VideoModule } from './video/video.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    UserModule,
    AuthModule,
    SequelizeModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        const env = configService.get<string>('NODE_ENV');
        if (env === 'production') {
          return {
            dialect: 'mysql' as Dialect,
            host: configService.get<string>('PRO_DB_HOST', 'localhost'), // 主机，默认为localhost
            port: configService.get<number>('PRO_DB_PORT', 3306), // 端口号
            username: configService.get<string>('PRO_DB_USERNAME', 'root'), // 用户名
            password: configService.get<string>('PRO_DB_KEY', 'root'), // 密码
            database: configService.get<string>('PRO_DB_NAME', 'test'), //数据库名
            define: {
              timestamps: true,
              underscored: false,
            },
            synchronize: false, //根据实体自动创建数据库表， 生产环境建议关闭
            logging: false,
            autoLoadModels: true,
          };
        } else {
          return {
            dialect: 'mysql' as Dialect, // 数据库类型
            host: configService.get<string>('DEV_DB_HOST', 'localhost'), // 主机，默认为localhost
            port: configService.get<number>('DEV_DB_PORT', 3306), // 端口号
            username: configService.get<string>('DEV_DB_USERNAME', 'root'), // 用户名
            password: configService.get<string>('DEV_DB_KEY', 'root'), // 密码
            database: configService.get<string>('DEV_DB_NAME', 'test'), //数据库名
            define: {
              timestamps: true,
              underscored: false,
            },
            synchronize: false, //根据实体自动创建数据库表， 生产环境建议关闭
            logging: false,
            autoLoadModels: true,
          };
        }
      },
    }),
    AuditModule,
    ProjectModule,
    VideoModule,
    ProjectVideoModule,
  ],
  controllers: [AppController],
  providers: [],
})
export class AppModule {}
